/**
 * 
 */
package org.vsg.common.pagination.wrapper;

import org.vsg.common.pagination.AdvancedPagination;
import org.vsg.common.pagination.Pagination;

/**
 * @author Bill Vison
 *
 */
public class AdvancedGenericWebSortByPaginationWrapper extends GenericWebSortByPaginationWrapper implements AdvancedWebPaginationWrapper{
	
	protected AdvancedPagination pagination;
	
	protected String goToGroupingParamName;
	

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.WebPaginationWrapper#loadPagination(org.vsg.commons.util.pagination.Pagination)
	 */
	public void loadPagination(Pagination pagination) throws PaginationWrapperException {
		// TODO Auto-generated method stub
		if (pagination instanceof AdvancedPagination) {
			this.pagination = (AdvancedPagination)pagination;
			
		}
		super.loadPagination(pagination);
	}

	/**
	 * @return Returns the goToGroupingParamName.
	 */
	public String getGoToGroupingParamName() {
		return goToGroupingParamName;
	}

	/**
	 * @param goToGroupingParamName The goToGroupingParamName to set.
	 */
	public void setGoToGroupingParamName(String goToGroupingParamName) {
		this.goToGroupingParamName = goToGroupingParamName;
	}
	
	public String getFristGroupingUrl() {

		int firstGroupIndex = 0;
		
		if (pagination.getTotalGrouping() > 0) {
			if (pagination.getFirstGrouping() == 0) {
				firstGroupIndex = 1;
			} else {
				firstGroupIndex = pagination.getFirstGrouping() + 1;
			}
		}		
		
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url );
		paramsUrlStr.append("&").append(goToGroupingParamName).append("=").append(firstGroupIndex);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());		
		
		return paramsUrlStr.toString();
	}
	
	public String getLastGroupingUrl() {
		int lastGroupIndex = 0;
		
		if (pagination.getTotalGrouping() > 0) {
			lastGroupIndex = pagination.getLastGrouping() + 1;
		}
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToGroupingParamName).append("=").append(lastGroupIndex);


		// add another parameter
		paramsUrlStr.append(getAdditionalParams());	
		
		return paramsUrlStr.toString();		
		
	}
	
	public String getPrevGroupingUrl() {
		
		int prevGroupUrl = 0;
		
		if (pagination.getTotalGrouping() > 0) {
			prevGroupUrl = (pagination.getGroupingIndex() - 1) + 1;
			if (pagination.getTotalPages() <= 1) {
				prevGroupUrl = 1;
			} else {
				if (prevGroupUrl < 1) {
					prevGroupUrl = 1;
				}
			}
		}
		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToGroupingParamName).append("=").append(prevGroupUrl);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();		
	}
	
	public String getNextGroupingUrl() {
		
		int nextGroupUrl = 0;
		
		if (pagination.getTotalGrouping() > 0) {
			if (pagination.getGroupingIndex() == 0) {
				
			}
			nextGroupUrl = (pagination.getGroupingIndex() + 1) + 1;
			if (pagination.getTotalGrouping() <=  1) {
				nextGroupUrl = 1;
			} else {
				if (nextGroupUrl > pagination.getLastGrouping()) {
					// add 1 is fix for the page viewer
					nextGroupUrl = pagination.getLastGrouping() + 1;
				}
			}
		}
		

		
		// if the url string is end with character "?" ,dont add the character "?" in the end
		String url = this.url.endsWith("?") ? this.url:(this.url+"?");
		
		StringBuffer paramsUrlStr = new StringBuffer(url);
		paramsUrlStr.append("&").append(goToGroupingParamName).append("=").append(nextGroupUrl);
		// add another parameter
		paramsUrlStr.append(getAdditionalParams());
		return paramsUrlStr.toString();				
	}


	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.AdvancedPaginationWrapper#getFirstGrouping()
	 */
	public int getFirstGrouping() {
		// TODO Auto-generated method stub
		return this.pagination.getFirstGrouping();
	}



	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.AdvancedPaginationWrapper#getGroupingIndex()
	 */
	public int getGroupingIndex() {
		// TODO Auto-generated method stub
		return this.pagination.getGroupingIndex();
	}



	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.AdvancedPaginationWrapper#getGroupingOfPageIndex(int)
	 */
	public int[] getGroupingOfPageIndex(int groupingNumber) {
		// TODO Auto-generated method stub
		return this.pagination.getGroupingOfPageIndex(groupingNumber);
	}



	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.AdvancedPaginationWrapper#getGroupingSize()
	 */
	public int getGroupingSize() {
		// TODO Auto-generated method stub
		return this.pagination.getGroupingSize();
	}



	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.wrapper.AdvancedPaginationWrapper#getLastGrouping()
	 */
	public int getLastGrouping() {
		// TODO Auto-generated method stub
		return this.pagination.getLastGrouping();
	}



}
